import request from "@/utils/request";

const BASE_URL = "/smtp";

const SmtpAPI = {
  getTemplateList() {
    return request<any, PageResult<EmailTemplate[]>>({
      url: `${BASE_URL}/emailTemplateList`,
      method: "get",
    });
  },

  getFormData() {
    return request<any, SmtpForm>({
      url: `${BASE_URL}/config`,
      method: "get",
    });
  },

  getByTemplateName(templateName: string) {
    return request<any, EmailTemplate>({
      url: `${BASE_URL}/getByTemplateName/${templateName}`,
      method: "get",
    });
  },

  updateContentByTemplateName(data: EmailTemplate) {
    return request({
      url: `${BASE_URL}/updateEmailTemplate`,
      method: "post",
      data: data,
    });
  },

  update(data: SmtpForm) {
    return request({
      url: `${BASE_URL}/update`,
      method: "post",
      data: data,
    });
  },

  testSmtp(email: string) {
    return request({
      url: `${BASE_URL}/test/${email}`,
      method: "get",
    });
  },
};

export default SmtpAPI;

/** role object form */
export interface SmtpForm {
  host: string;
  port: number;
  username: string;
  password: string;
  fromEmail: string;
  auth: boolean;
  starttlsEnable: boolean;
  startsslEnable: boolean;
  serviceName: string;
  serviceMail: string;
}

export interface EmailTemplate {
  templateName: string;
  subject: string;
  createdAt?: Date;
  content: string;
}
